/* CursesConsole.h
Michael Zahniser, 2008

Header file for the CursesConsole class, which responds to a line of text by printing a response to a given output stream. The CursesConsole is the main controller class in the command-line version of the Gringotts financial management program.
*/

#ifndef __GRINGOTTS_CURSES_CONSOLE_H__
#define __GRINGOTTS_CURSES_CONSOLE_H__

#include "Database.h"
#include "Account.h"
#include "AccountType.h"
#include "Ledger.h"
#include "_ncurses/TextTree.h"
class Command;

#include <string>
#include <map>
#include <vector>

using std::string;
using std::map;
using std::vector;



class CursesConsole {
public:
    // When a console is constructed, the database is loaded automatically.
    CursesConsole();
    // When the console is destructed, save the modified data.
    ~CursesConsole();

    // Run the console.
    void Run();


private:
    // Draw the tree.
    TextTree::Iterator DrawTree();

    // Handle tree-navigation events.
    // Return false if the given key is not for navigation.
    bool HandleTree(int key);
    // Handle text entry events. Return false if the user enters
    // a quit command.
    bool HandleCommands(int key);
    // Add the given string to the tree, breaking and indenting at
    // every '\n' in the string.
    void ShowText(const string &text);
    // Clear the screen (and the tree).
    void ClearScreen();
    
    // Handlers for different command types.
    void DoBalance(const Command &command);
    void DoChecks();
    void DoInfo(const Command &command);
    void DoRecords(const Command &command);
    void DoTransact();
    void DoCancel();
    
    // Add to the vector of ledgers.
    void AddLedger(const map<string, Account> &accounts,
		   AccountType accountType,
		   const Command &command);


private:
    Database database;

    vector<Ledger> ledgers;
    
    TextTree tree;
    TextTree::Iterator treeIt;
    int scrollRow;
    int selectedRow;
    int rows;
    int cols;

    string line;

    string databasePath;
};

#endif
